<template>
  <div class="video_resource_container">
    <el-row>
      <el-col :span="24">
        <el-button
          v-if="
            videoInfo.isSingle === '1' ||
            (videoInfo.isSingle === '0' &&
              (!pagination.resourceDatas ||
                pagination.resourceDatas.length < 1))
          "
          type="primary"
          @click="handleClick(null, 0, '新增剧集')"
          >新增</el-button
        >
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :before-close="dramaSeriesFormHandleClose"
          width="40%"
        >
          <el-form
            label-position="left"
            label-width="100px"
            :model="dramaSeriesForm"
            :rules="rules"
            ref="dramaSeriesForm"
          >
            <el-form-item label="名称" prop="name">
              <el-input
                :disabled="formType === 1"
                v-model="dramaSeriesForm.name"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item prop="type" label="类型">
              <el-radio
                v-for="item in typeDicts"
                :key="item.id"
                v-model="dramaSeriesForm.type"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-form-item>
            <el-form-item prop="sort" label="排序">
              <el-input-number
                :disabled="formType === 1"
                v-model="dramaSeriesForm.sort"
                :min="1"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="剧集状态">
              <el-tag disable-transitions>{{
                getStatusLabel(dramaSeriesForm.status)
              }}</el-tag>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              v-if="formType !== 1"
              :loading="loading"
              type="primary"
              @click="confirmHandel('dramaSeriesForm')"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="pagination.resourceDatas"
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
        >
          <el-table-column type="index" width="30px"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="tyoe" label="类型" width="100px">
            <template slot-scope="scope">
              <el-tag disable-transitions>{{
                getTypeLabel(scope.row.type)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="sort"
            label="排序"
            width="100px"
          ></el-table-column>
          <el-table-column prop="status" label="状态" width="100px">
            <template slot-scope="scope">
              <el-tag disable-transitions>{{
                getStatusLabel(scope.row.status)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200px">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status === '1'"
                @click="release(scope.row.id)"
                type="text"
                size="small"
                >发布</el-button
              >
              <el-button
                @click="checkResource(scope.row)"
                type="text"
                size="small"
                >视频资源</el-button
              >
              <el-button
                v-if="scope.row.status === '0'"
                @click="release(scope.row.id)"
                type="text"
                size="small"
                >取消发布</el-button
              >
              <el-button
                v-if="scope.row.status === '1'"
                @click="handleClick(scope.row, 2, '剧集详情')"
                type="text"
                size="small"
                >编辑</el-button
              >

              <el-popconfirm
                @onConfirm="delDramaSeries(scope.row.id)"
                title="这是一段内容确定删除吗？"
              >
                <el-button
                  slot="reference"
                  style="margin-left: 10px"
                  v-if="scope.row.status !== '0'"
                  onConfirm="delDramaSeries(scope.row.id)"
                  type="text"
                  size="small"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizeChoose"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="playerVisible" width="30%">
      <Player v-if="playerVisible" />
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPaginationList,
  addDramaSeries,
  updateDramaSeries,
  delDramaSeries,
  release
} from "@/api/video/dramasSeries";
import Player from "../player";
export default {
  name: "Administrator",
  components: {
    Player,
  },
  created() {
    this.getPaginationVideoResource();
    this.getDictList({ type: "video_drama_series_status" }).then((res) => {
      this.statusOptions = res.data;
    });
    this.getDictList({ type: "video_drama_series_type" }).then((res) => {
      this.typeDicts = res.data;
    });
  },
  props: {
    videoInfo: {
      required: true,
    },
  },
  data() {
    return {
      typeDicts: [],
      playerVisible: false,
      statusOptions: [],
      formType: 0,
      loading: false,
      dialogVisible: false,
      dialogTitle: "",
      dramaSeriesForm: {
        files: [],
        type: "0",
        name: "",
        videoInfoId: this.videoInfo.id,
        url: "",
        contentType: "",
        sort: null,
        status: "1",
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizeChoose: [5, 10, 15, 20],
        total: 0,
        resourceDatas: [],
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },

  watch: {
    dialogVisible: function () {
      if (this.dialogVisible === false) {
        this.$refs["dramaSeriesForm"].resetFields();
      }
    },
  },
  methods: {
    delDramaSeries(id) {
      delDramaSeries(id).then(() => {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.getPaginationVideoResource();
      });
    },
    checkResource(data) {
      this.$emit("checkResource", data);
    },
    dramaSeriesFormHandleClose() {
      if (!this.dramaSeriesForm.id) {
        this.$confirm("还未新增成功，确认关闭？")
          .then(() => {
            this.dialogVisible = false;
          })
          .catch(() => {});
      }
    },
    release(id){
      release(id).then(() => {
        this.$message({
          message: "发布成功",
          type: "success",
        });
        this.getPaginationVideoResource();
      });
    },

    getStatusLabel(value) {
      let ss = "";
      this.statusOptions.forEach((item) => {
        if (item.value === value) {
          ss = item.label;
        }
      });
      return ss;
    },
    getTypeLabel(value) {
      let ss = "";
      this.typeDicts.forEach((item) => {
        if (item.value === value) {
          ss = item.label;
        }
      });
      return ss;
    },
    addDramaSeries() {
      addDramaSeries(this.dramaSeriesForm)
        .then(() => {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationVideoResource();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    updateDramaSeries() {
      updateDramaSeries(this.dramaSeriesForm)
        .then(() => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationVideoResource();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    confirmHandel(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formType === 2) {
            this.updateDramaSeries();
          } else {
            this.addDramaSeries();
          }
          this.loading = true;
        }
      });
    },
    getPaginationVideoResource() {
      const params = {
        pageSize: this.pagination.pageSize,
        pageIndex: this.pagination.currentPage,
        videoInfoId: this.dramaSeriesForm.videoInfoId,
      };
      getPaginationList(params).then((res) => {
        this.pagination.resourceDatas = res.data.records;
        this.pagination.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val;
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val;
    },
    handleClick(data, type, title) {
      if (type !== 0) {
        this.dramaSeriesForm = data;
      } else {
        this.dramaSeriesForm.checkRoleIds = [];
      }
      this.dialogTitle = title;
      this.formType = type;
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.menu-container {
  .el-row {
    margin-bottom: 20px;
  }
}
</style>